package com.ruoyi.customer.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.customer.domain.CustomerContract;
import com.ruoyi.customer.dto.AttachmentDto;
import com.ruoyi.customer.dto.AttachmentInfo;
import com.ruoyi.customer.dto.CustomerContractDTO;
import com.ruoyi.customer.service.CustomerContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 客户合同
 *
 * @Author:zzm
 * @Date: 2024/8/29 15:26
 */
@RestController
@RequestMapping("/amac/customerContract")
public class CustomerContractController extends BaseController {

    @Autowired
    private CustomerContractService customerContractService;

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageUtils.startPage();
    }

    /**
     * 查询列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerContractDTO dto) {
        startPage();
        List<CustomerContract> list = customerContractService.getList(dto);
        return getDataTable(list);
    }

    /**
     * 新增
     */
    @PutMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody CustomerContract ro) {
        return toAjax(customerContractService.addOrUpdate(ro));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public AjaxResult deleteByIds(@PathVariable Long[] ids) {
        customerContractService.delete(ids);
        return AjaxResult.success(true);
    }

    //附件接口-合同
    @GetMapping("/attachmentList")
    public ResponseEntity<AjaxResult> getAttachmentList(@RequestParam @NotNull String parentId) {

        try {
            List<AttachmentInfo> attachments = customerContractService.getAttachmentList(parentId);

            if (attachments == null || attachments.isEmpty()) {
                return ResponseEntity.ok(AjaxResult.success(Collections.emptyList()));
            }

            return ResponseEntity.ok(AjaxResult.success(attachments));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(AjaxResult.error("参数错误: " + e.getMessage()));
        } catch (Exception e) {
            //log.error("获取附件列表失败: parentId={}, date={}", parentId, date, e);
            return ResponseEntity.internalServerError()
                    .body(AjaxResult.error("获取附件列表失败，请稍后重试"));
        }
    }

    /**
     * 保存或更新附件
     */
    @PostMapping("/save")
    public AjaxResult saveAttachments(@RequestBody AttachmentDto request) {
        boolean result = customerContractService.saveOrUpdateAttachments(
                request.getParentId(),
                request.getAttachments()
        );
        return result ? AjaxResult.success() : AjaxResult.error("操作失败");
    }

    //附件接口-合同报价
    @GetMapping("/attachmentListPrice")
    public ResponseEntity<AjaxResult> getAttachmentListPrice(@RequestParam @NotNull String parentId) {

        try {
            List<AttachmentInfo> attachments = customerContractService.getAttachmentListPrice(parentId);

            if (attachments == null || attachments.isEmpty()) {
                return ResponseEntity.ok(AjaxResult.success(Collections.emptyList()));
            }

            return ResponseEntity.ok(AjaxResult.success(attachments));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(AjaxResult.error("参数错误: " + e.getMessage()));
        } catch (Exception e) {
            //log.error("获取附件列表失败: parentId={}, date={}", parentId, date, e);
            return ResponseEntity.internalServerError()
                    .body(AjaxResult.error("获取附件列表失败，请稍后重试"));
        }
    }

    /**
     * 保存或更新附件-报价
     */
    @PostMapping("/savePrice")
    public AjaxResult saveAttachmentsPrice(@RequestBody AttachmentDto request) {
        boolean result = customerContractService.saveOrUpdateAttachmentsPrice(
                request.getParentId(),
                request.getAttachments()
        );
        return result ? AjaxResult.success() : AjaxResult.error("操作失败");
    }

//    /**
//     * 批量查询附件
//     */
//    @PostMapping("/batchQuery")
//    public AjaxResult batchQueryAttachments(@RequestBody List<Long> parentIds) {
//        Map<Long, List<AttachmentInfo>> result = customerContractService.getAttachmentsByParentIds(parentIds);
//        return AjaxResult.success(result);
//    }
}
